<?php
require_once ('..'.DIRECTORY_SEPARATOR.'initialize.php');
include_once(Utils::getPath(array('shared', 'template.php')));
include_once(Utils::getPath(array('repositories', 'userRepository.php')));

$template = new Template('application');
$template->initialize();

$userRep = new UserRepository();

$page = !isset($_GET['page']) ? 0 : ($_GET['page'] == 0 ? 0 : $_GET['page'] - 1);
$filterUrlParam = '';

if (isset($_GET['filter'])) {
    if ($_GET['filter'] == 'friends') {
        $users = $userRep->getFriends($_SESSION['authenticatedId'], true, $page);
        $filterUrlParam = '&amp;filter=friends';
        $count = $userRep->countFriends($_SESSION['authenticatedId'], true);
        $title = "Friends";
    } else if ($_GET['filter'] == 'requests') {
        $users = $userRep->getRequests($_SESSION['authenticatedId'], $page);
        $filterUrlParam = '&amp;filter=requests';
        $count = $userRep->countRequests($_SESSION['authenticatedId']);
        $title = "Requests";
    }
} else {
    $users = $userRep->getAll(null, null, $page);
    $count = $userRep->countLast();
    $title = "All people";
}

$pageMax = ceil($count / 10);

$userList = array();
if ($users != null) {
    foreach ($users as $user) {
        $userId = $user->getId();
        if ($userId != $_SESSION['authenticatedId']) {
            $state = $userRep->getFriendsState($_SESSION['authenticatedId'], $userId);

            $userElement = array();
            $userElement['image'] = $user->getImage();
            $userElement['name'] = $user->getName();
            $userElement['username'] = $user->getUserName();
            $userElement['email'] = $user->getEmail();
            $userElement['description'] = Utils::splitString($user->getDescription(), 90);
            $userElement['url'] = "profile.php?id=$userId";

            if (!is_null($state)) {
                if ($state[0])
                    $userElement['state'] = 'FRIEND - <a href="requests.php?delete=' . $userId . '">Delete</a>';
                else {
                    $userElement['state'] = 'PENDING - ';
                    if ($state[1])
                        $userElement['state'] .= '<a href="requests.php?add=' . $userId . '">Accept</a> ';
                    $userElement['state'] .= '<a href="requests.php?delete=' . $userId . '">Cancel</a>';
                }
            }else
                $userElement['state'] = '<a href="requests.php?add=' . $userId . '">Send invitation</a>';

            $userList[] = $userElement;
        }
    }
}else {
    if ($_GET['filter'] == 'friends')
        $emptyMessage = 'No friends :(';
    else if ($_GET['filter'] == 'requests')
        $emptyMessage = 'No requests';
    else
        $emptyMessage = 'No users';
}
?>
<h2><?php echo $title; ?></h2>
<ul id="user-filter-mode">
    <li><a href="list.php">All</a></li>
    <li><a href="list.php?filter=friends">Friends</a></li>
    <li><a href="list.php?filter=requests">Requests</a></li>
</ul>
<?php
if (count($userList) == 0)
    echo $emptyMessage;
else {
?>
    <table id="user-list">
    <?php
    foreach ($userList as $user) {
        ?>
        <tr class="user-element">
            <td class ="user-image"><img alt="User avatar" src="<?php echo $user['image'] ?>" /></td>
            <td class ="user-info">
                <div><a href="<?php echo $user['url'] ?>" ><?php echo $user['name'] ?></a> - <?php echo $user['username'] ?></div>
                <div class ="user-email"><?php echo $user['email'] ?></div>
                <div class ="user-description"><?php echo $user['description'] ?></div>
            </td>
            <td class ="user-state"><?php echo $user['state'] ?></td>
        </tr>
        <?php
    }
}
?>
</table>
<?php
Utils::pagination($page, $pageMax, Utils::getUrl('user/list.php'), $filterUrlParam);

$template->createPage($title);
?>